package com.dycx.common;

import com.dycx.common.constants.RegexpConstants;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Common {

    /**
     * 生成随机密码生成方式一
     * 密码字典 -> 随机获取字符
     *
     * @param len 生成密码长度
     * @return
     */
    public static String createRandom(int len, Integer type) {
        int i; //生成的随机数
        int count = 0; //生成的密码的长度

        // 密码字典
        char[] str = {};
        if (type == 1) {
            str = new char[]{
                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            };
        }

        if (type == 2) {
            str = new char[]{
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            };
        }

        StringBuilder stringBuffer = new StringBuilder("");
        Random r = new Random();
        while (count < len) {
            //生成 0 ~ 密码字典-1之间的随机数
            i = r.nextInt(str.length);
            stringBuffer.append(str[i]);
            count++;
        }
        return stringBuffer.toString();
    }

    public static List<String> getImageUrl(String HTML) {

        Matcher matcher = Pattern.compile(RegexpConstants.IMAGE_URL).matcher(HTML);
        List<String> listImgUrl = new ArrayList<>();
        while (matcher.find()) {
            listImgUrl.add(matcher.group());
        }
        return listImgUrl;
    }



}
